package com.aidex.admin.controller.monitor;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.query.QueryGenerator;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.framework.web.service.SysPasswordService;
import com.aidex.system.domain.SysLoginLog;
import com.aidex.system.service.SysLoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 系统访问记录
 */
@RestController
@RequestMapping("/monitor/loginLog")
public class SysLoginLogController extends BaseController<SysLoginLogService> {

    @Autowired
    private SysPasswordService passwordService;

    @PreAuthorize("@ss.hasPermi('monitor:loginLog:list')")
    @GetMapping("/page")
    public R page(SysLoginLog entity,
                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                  HttpServletRequest req) {
        QueryWrapper<SysLoginLog> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        queryWrapper.orderByDesc("login_time");
        Page<SysLoginLog> page = new Page<>(pageNo, pageSize);
        IPage<SysLoginLog> pageList = service.selectPage(page, queryWrapper);
        return R.data(pageList);
    }

    @PreAuthorize("@ss.hasPermi('monitor:loginLog:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public R unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginRecordCache(userName);
        return R.status(true);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:loginLog:export')")
    @GetMapping("/export")
    public R export(SysLoginLog loginLog, HttpServletRequest req) {
        QueryWrapper<SysLoginLog> queryWrapper = QueryGenerator.initQueryWrapper(loginLog, req.getParameterMap());
        List<SysLoginLog> list = service.selectList(queryWrapper);
        ExcelUtil<SysLoginLog> util = new ExcelUtil<SysLoginLog>(SysLoginLog.class);
        return util.exportExcel(list, "登录日志");
    }

    @PreAuthorize("@ss.hasPermi('monitor:loginLog:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(service.deleteBatchIds(Arrays.asList(ids)));
    }

    @PreAuthorize("@ss.hasPermi('monitor:loginLog:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R clean() {
        service.clean();
        return R.status(true);
    }
}
